//
//  MineSetViewController.m
//  Mahjong
//
//  Created by KING on 16/8/31.
//  Copyright © 2016年 swift. All rights reserved.
//

#import "MineSetViewController.h"
#import "Header.h"
#import "PMElasticRefresh.h"
#import "AddPhoneViewController.h"
#import "EditPassViewController.h"


#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
@interface MineSetViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
    UIImagePickerController *_imagePickerController;
    UIImageView *imageView;
}
@property (nonatomic, strong) UITableView *myTableView;



@end

@implementation MineSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.navigationController.navigationBar.translucent = YES;

    self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.myTableView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.tableFooterView = [[UIView alloc]init];
//    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
//    [self.myTableView pm_RefreshHeaderWithBlock:^{
//        
//        NSLog(@"refreshBlock");
//    }];

    [self createButton];
   
    self.navigationItem.title = @"设置";
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_but_back"] style:(UIBarButtonItemStyleDone) target:self action:@selector(backAction)];
    backItem.tintColor = [UIColor grayColor];
    self.navigationItem.leftBarButtonItem = backItem;
    
    _imagePickerController = [[UIImagePickerController alloc] init];
    _imagePickerController.delegate = self;
    _imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    _imagePickerController.allowsEditing = YES;
    
}

- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)createButton{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, SCREENH_HEIGHT-40, SCREEN_WIDTH, 40);
    button.backgroundColor = [UIColor redColor];
    [button setTitle:@"退出登录" forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(disAction:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)disAction:(UIButton *)btn{
//    [self.myTableView endRefresh];
//    AddPhoneViewController *add = [[AddPhoneViewController alloc] init];
//    [self.navigationController pushViewController:add animated:YES];
    [self.navigationController popViewControllerAnimated:YES];
    [[NSUserDefaults standardUserDefaults] setObject:@"no" forKey:@"login"];

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section ==1) {
        return 1;
    }
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
            
            
            imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 5, 50, 50)];
            imageView.image = [UIImage imageNamed:@"abc.jpg"];
            imageView.layer.cornerRadius = 25;
            imageView.layer.masksToBounds = YES;
            [cell.contentView addSubview:imageView];
            
            UIButton *la = [UIButton buttonWithType:UIButtonTypeCustom];
             la.frame = CGRectMake(SCREEN_WIDTH-100, 20, 100, 30);
//           la.textAlignment = NSTextAlignmentRight;
            [la setTitle:@"更换头像" forState:UIControlStateNormal];
            [la setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//           la.titleLabel.textColor = [U/IColor blackColor];
            [la addTarget:self action:@selector(imaAction) forControlEvents:UIControlEventTouchUpOutside];
            [cell.contentView addSubview:la];
            return cell;
        }else{
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
            cell.textLabel.text = @"熊大麻将馆";
            UILabel *la = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH-10, 20)];
            la.textAlignment = NSTextAlignmentRight;
            la.text = @"修改昵称  ";
            [cell addSubview:la];
            return cell;
        }

        
    }
    
    if (indexPath.section == 1) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        cell.textLabel.text = @"修改密码";
        UIImageView *la = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-30, 10, 10, 20)];
        la.image = [UIImage imageNamed:@"but_go.png"];
        [cell addSubview:la];
        return cell;
    }
    
    if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
            cell.textLabel.text = @"法律条款";
            UIImageView *la = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-30, 10, 10, 20)];
            la.image = [UIImage imageNamed:@"but_go.png"];
            [cell addSubview:la];

            return cell;
        }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        cell.textLabel.text = @"关于我们";
            UIImageView *la = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-30, 25, 10, 20)];
            la.image = [UIImage imageNamed:@"but_go.png"];
            [cell addSubview:la];

        return cell;
       }
    }
    else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        return cell;
        
    }
    
}
#pragma mark 从相册获取图片或视频
- (void)selectImageFromAlbum
{
    NSLog(@"相册");
    _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self presentViewController:_imagePickerController animated:YES completion:nil];
}
#pragma mark UIImagePickerControllerDelegate
//该代理方法仅适用于只选取图片时
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo {
    imageView.image = image;
    //压缩图片
    NSData *fileData = UIImageJPEGRepresentation(imageView.image, 1.0);
    NSString *encodedString = [fileData base64Encoding];

    [self postImage:encodedString];
    [self dismissViewControllerAnimated:YES completion:nil];

//    NSLog(@"选择完毕----image:%@-----info:%@---%@",image,editingInfo,fileData);
}
-(void)postImage:(NSString *)dat{
//    NSDictionary *dic = @{@"commentid":@"4",@"Code":dat,@"Userid":USERID};
//    [AppTools postData:@"http://api03.m0571.com/WebJson.aspx?book=InsertcommentImage&" from:^(id data) {
//        
//        //        [data appendPartWithFileData:UIImagePNGRepresentation(dat) name:@"Code" fileName:@"code.png" mimeType:@"image/png"];
//        
//    } dic:dic success:^(id result) {
//        NSLog(@"%@%@",result,[result[0] objectForKey:@"describe"]);
//        
//    } error:^(NSError *error) {
//        NSLog(@"shibai");
//        
//        
//    }];
    
    NSDictionary *dic = @{@"Type":@"imageurl",@"Code":dat,@"Userid":@"25"};
    [AppTools postData:@"http://api03.m0571.com/WebJson.aspx?book=upimages&" from:^(id data) {
    
//////        [data appendPartWithFileData:UIImagePNGRepresentation(dat) name:@"Code" fileName:@"code.png" mimeType:@"image/png"];
        
    } dic:dic success:^(id result) {
        NSLog(@"%@%@",result,[result[0] objectForKey:@"describe"]);
        
    } error:^(NSError *error) {
        NSLog(@"shibai");

        
    }];
//    [AppTools postURL:[NSString stringWithFormat:@"http://api03.m0571.com/WebJson.aspx?book=upimages&"] data:dic success:^(id result) {
//        NSLog(@"%@",result);
//        
//    } error:^(NSError *error) {
//        
//        
//    }];
}
//适用获取所有媒体资源，只需判断资源类型
//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//    NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
//    //判断资源类型
//    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]){
//        //如果是图片
//        self.imageView.image = info[UIImagePickerControllerEditedImage];
//        //压缩图片
//        NSData *fileData = UIImageJPEGRepresentation(self.imageView.image, 1.0);
//        //保存图片至相册
//        UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
//        //上传图片
//        [self uploadImageWithData:fileData];
//
//    }else{
//        //如果是视频
//        NSURL *url = info[UIImagePickerControllerMediaURL];
//        //播放视频
//        _moviePlayer.contentURL = url;
//        [_moviePlayer play];
//        //保存视频至相册（异步线程）
//        NSString *urlStr = [url path];
//
//        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//            if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlStr)) {
//
//                UISaveVideoAtPathToSavedPhotosAlbum(urlStr, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
//            }
//        });
//        NSData *videoData = [NSData dataWithContentsOfURL:url];
//        //视频上传
//        [self uploadVideoWithData:videoData];
//    }
//    [self dismissViewControllerAnimated:YES completion:nil];
//}
-(void)imaAction{
    [self selectImageFromAlbum];

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            [self selectImageFromAlbum];

        }

    }
    if (indexPath.section == 1) {
        EditPassViewController *edit = [[EditPassViewController alloc] init];
        [self.navigationController pushViewController:edit animated:YES];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 70;
        }
    }
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
